package com.advertisementsystem.adservice.mapper;

import com.advertisementsystem.adservice.entity.Ad;
import com.advertisementsystem.adservice.entity.AdCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdMapper {

    // Ad操作
    @Select("SELECT a.*, c.name as categoryName FROM ad a LEFT JOIN ad_category c ON a.category_id = c.id")
    List<Ad> findAll();

    @Select("SELECT a.*, c.name as categoryName FROM ad a LEFT JOIN ad_category c ON a.category_id = c.id WHERE a.id = #{id}")
    Ad findById(Integer id);

    @Insert("INSERT INTO ad(name, category_id, image_url, link_url, status) VALUES(#{name}, #{categoryId}, #{imageUrl}, #{linkUrl}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Ad ad);

    @Update("UPDATE ad SET name=#{name}, category_id=#{categoryId}, image_url=#{imageUrl}, link_url=#{linkUrl}, status=#{status} WHERE id=#{id}")
    int update(Ad ad);

    @Delete("DELETE FROM ad WHERE id=#{id}")
    int delete(Integer id);

    // AdCategory操作
    @Select("SELECT * FROM ad_category")
    List<AdCategory> findAllCategories();

    @Select("SELECT * FROM ad_category WHERE id = #{id}")
    AdCategory findCategoryById(Integer id);

    @Insert("INSERT INTO ad_category(name, description) VALUES(#{name}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCategory(AdCategory category);

    @Update("UPDATE ad_category SET name=#{name}, description=#{description} WHERE id=#{id}")
    int updateCategory(AdCategory category);

    @Delete("DELETE FROM ad_category WHERE id=#{id}")
    int deleteCategory(Integer id);
}